package com.libraries.image.activity;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.libraries.base.BaseActivity;
import com.libraries.helper.ImgHelper;
import com.libraries.image.entities.PreviewInfo;
import com.libraries.image.view.PreviewImgView;
import com.example.libraries.R;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;

@SuppressLint("NewApi")
public class ImagePreviewActivity extends BaseActivity {
	
	private int statusHeight;// 状态栏的高度
	private int windowW, windowH;

	private PreviewInfo previewInfo;
	private PreviewImgView previewView;
	private ViewTreeObserver viewTreeObserver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initPreviewData();
		initPreviewView();
		setContentView(previewView);
	}

	@SuppressWarnings("deprecation")
	private void initPreviewData() {
		windowW = getWindowManager().getDefaultDisplay().getWidth();
		windowH = getWindowManager().getDefaultDisplay().getHeight();
		previewInfo = (PreviewInfo) getIntent().getSerializableExtra("previewInfo");
	}

	private void initPreviewView() {
		previewView =new PreviewImgView(this);
		previewView.setBackgroundResource(R.color.translucent);
		previewView.setOriginalInfo(previewInfo.getImgWidth(),
		previewInfo.getImgHeight(), previewInfo.getLocationX(),
		previewInfo.getLocationY());
		previewView.transformIn();

		previewView.setLayoutParams(new LayoutParams(-1,-1));
		previewView.setActivity(this);// 注入Activity.
		/** 测量状态栏高度 **/
		viewTreeObserver = previewView.getViewTreeObserver();
		viewTreeObserver
				.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						if (statusHeight == 0) {
							// 获取状况栏高度
							Rect frame = new Rect();
							getWindow().getDecorView()
									.getWindowVisibleDisplayFrame(frame);
							statusHeight = frame.top;
							previewView.setScreen_H(windowW - statusHeight);
							previewView.setScreen_W(windowH);
						}

					}
				});
		ImgHelper.loadFileByFresco(previewView, previewInfo.getImgPath(), new ControllerListener<ImageInfo>() {

			@Override
			public void onSubmit(String id, Object callerContext) {
			}

			@Override
			public void onFinalImageSet(String id, ImageInfo imageInfo,
					Animatable animatable) {
				previewView.setPreviewSize(imageInfo.getWidth(), imageInfo.getHeight());
			}

			@Override
			public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
			}

			@Override
			public void onIntermediateImageFailed(String id, Throwable throwable) {
			}

			@Override
			public void onFailure(String id, Throwable throwable) {
				ImgHelper.loadResByFresco(previewView, R.drawable.ic_default_banner, null);
			}

			@Override
			public void onRelease(String id) {
			}
		});
	}

	@Override
	public void onBackPressed() {
		previewView.setOnTransformListener(new PreviewImgView.TransformListener() {
			@Override
			public void onTransformComplete(int mode) {
				if (mode == 2) {
					finish();
				}
			}
		});
		previewView.transformOut();
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (isFinishing()) {
			overridePendingTransition(0, 0);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
